﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    /// <summary>
    /// 该类依赖7z.dll 或者7z64.dll 如果提示缺少这两个文件的话，请手动拷贝这两个文件到应用程序目录下。
    /// </summary>
    public static class ZipUtil
    {
        /// <summary>
        /// 使用zip压缩一个目录
        /// </summary>
        /// <param name="sourceDirectory"></param>
        /// <param name="destFullName"></param>
        /// <param name="rewrite"></param>
        public static void CompressDirectory(String sourceDirectory, String destFullName, bool rewrite= false)
        {
            try
            {
                SevenZip.SevenZipCompressor tmp = new SevenZip.SevenZipCompressor();
                tmp.ArchiveFormat = SevenZip.OutArchiveFormat.Zip;
                System.IO.FileInfo outputFileInfo = new System.IO.FileInfo(destFullName);
                if (outputFileInfo.Exists)
                {
                    if (rewrite)
                    {
                        System.IO.File.Delete(outputFileInfo.FullName);
                    }
                    else
                    {
                        throw new Exception("文件已经存在！");
                    }
                }
                outputFileInfo.Directory.Create();
                tmp.CompressDirectory(sourceDirectory, outputFileInfo.FullName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
